package utils

import (
	"bytes"
	"io"
	"io/ioutil"
	"local.public/innerpkg/logger"
	"net/http"
	"reflect"
	"strconv"
	"strings"
	"time"
)

func RemoveFirstSlash(sPath string) string {
	tmpPath := sPath
	for {
		if strings.HasPrefix(tmpPath, "/") {
			lenPath := len(tmpPath)
			tmpPath = tmpPath[1:lenPath]
			continue
		}
		break
	}
	return tmpPath
}

func RemoveLastSlash(sPath string) string {
	tmpPath := sPath
	for {
		if strings.HasSuffix(tmpPath, "/") {
			lenPath := len(tmpPath)
			tmpPath = tmpPath[0 : lenPath-1]
			continue
		}
		break
	}
	return tmpPath
}

func RemoveLastUnderline(sPath string) string {
	tmpPath := sPath
	for {
		if strings.HasSuffix(tmpPath, "_") {
			lenPath := len(tmpPath)
			tmpPath = tmpPath[0 : lenPath-1]
			continue
		}
		break
	}
	return tmpPath
}

func ExcludeRootUrlPathPrefix(sPath string, sRootUrlPath string) string {
	tmpPath := sPath
	for {
		if strings.HasPrefix(tmpPath, sRootUrlPath) {
			lenWord := len(sRootUrlPath)
			tmpPath = tmpPath[lenWord:]
			break
		}
		break
	}
	return tmpPath
}

func ExcludeStaticPrefix(sPath string) string {
	tmpPath := sPath
	for {
		if strings.HasPrefix(tmpPath, "/static3") {
			lenStatic := len("/static3")
			tmpPath = tmpPath[lenStatic:]
			break
		}
		if strings.HasPrefix(tmpPath, "static3") {
			lenStatic := len("static3")
			tmpPath = tmpPath[lenStatic:]
			break
		}
		if strings.HasPrefix(tmpPath, "/static2") {
			lenStatic := len("/static2")
			tmpPath = tmpPath[lenStatic:]
			break
		}
		if strings.HasPrefix(tmpPath, "static2") {
			lenStatic := len("static2")
			tmpPath = tmpPath[lenStatic:]
			break
		}
		if strings.HasPrefix(tmpPath, "/static") {
			lenStatic := len("/static")
			tmpPath = tmpPath[lenStatic:]
			break
		}
		if strings.HasPrefix(tmpPath, "static") {
			lenStatic := len("static")
			tmpPath = tmpPath[lenStatic:]
			break
		}
		break
	}
	return tmpPath
}

func ExcludeWordPrefix(sPath string, sWord string) string {
	tmpPath := sPath
	for {
		if strings.HasPrefix(tmpPath, "/"+sWord) {
			lenWord := len("/" + sWord)
			tmpPath = tmpPath[lenWord:]
			break
		}
		if strings.HasPrefix(tmpPath, sWord) {
			lenWord := len(sWord)
			tmpPath = tmpPath[lenWord:]
			break
		}
		break
	}
	return tmpPath
}

func GetStringFromMapInterface(mMap *map[string]interface{}, sKey string) string {
	if iValue, ok := (*mMap)[sKey]; ok {
		val := reflect.ValueOf(iValue) //获取reflect.Type类型
		kd := val.Kind()               //获取到a对应的类别

		if kd == reflect.String {
			return iValue.(string)
		} else if kd == reflect.Float64 {
			return strconv.Itoa(int(iValue.(float64)))
		} else {
			return ""
		}
	}
	return ""
}

func HttpPostRequest(url string, head map[string]string, body []byte) ([]byte, error) {
	var payload io.Reader
	payload = bytes.NewReader(body)
	req, _ := http.NewRequest("POST", url, payload)
	logger.Debug("http req url is " + url)
	for key, value := range head {
		req.Header.Add(key, value)
	}

	client := &http.Client{
		Timeout: 7 * time.Second,
	}

	res, err1 := client.Do(req)
	if err1 != nil {
		logger.Debug("HttpJsonRequest 请求失败!", err1)
		return nil, err1
	}

	defer res.Body.Close()
	retBody, err2 := ioutil.ReadAll(res.Body)
	if err2 != nil {
		logger.Debug("HttpJsonRequest读取响应失败!", err2)
		return nil, err2
	}
	return retBody, nil
}

func LimitIntRange(iValue, iLowValue, iHighValue int) int {
	if iValue > iHighValue {
		return iHighValue
	}
	if iValue < iLowValue {
		return iLowValue
	}
	return iValue
}

func Decimal(value float64, prec int) float64 {
	value, _ = strconv.ParseFloat(strconv.FormatFloat(value, 'f', prec, 64), 64)
	return value
}
